package minh.bac.wcbrazil2014.favorite;

import java.util.ArrayList;

import minh.bac.wcbrazil2014.R;
import minh.bac.wcbrazil2014.WCTAG;
import minh.bac.wcbrazil2014.broadcastreceiver.AlarmReceiver;
import minh.bac.wcbrazil2014.object.FootBallTeamObj;
import minh.bac.wcbrazil2014.object.MatchScheduleObj;
import minh.bac.wcbrazil2014.object.VongBangObj;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

public class FavoriteItemChildLV extends Fragment{
	TextView date,kenhTV,groupDetail,doi1,time,doi2;
	ImageView  imgDoi1, imgDoi2;
	ImageButton imageButtonArlam, imageBtBack;
	DisplayImageOptions displayImageOptions;
	ArrayList<FootBallTeamObj> ballTeamObj, ballTeamObj2;
	AlarmReceiver alarmReceiver;
	String nameTeam, imgLink;
	
	//private int mTypeShow;
	MatchScheduleObj matchScheduleObj;
	public static FavoriteItemChildLV newInstance(String nameTeam, String imgLink)
	{
		FavoriteItemChildLV itemListViewFragment = new FavoriteItemChildLV();
		Bundle bundle = new Bundle();
		bundle.putString(WCTAG.KEY_NAMETEAM, nameTeam);
		bundle.putString(WCTAG.KEY_IMGLINK_TEAM, imgLink);
		itemListViewFragment.setArguments(bundle);
		return itemListViewFragment;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.favorite_item_listviewchlid
				, container, false);
		nameTeam = getArguments().getString(WCTAG.KEY_NAMETEAM);
		//
		//mTypeShow =  getArguments().getInt(WCTAG.TYPE_SHOW);
		//
		
		//
		date = (TextView) rootView.findViewById(R.id.date_detail_favorite_child);
		kenhTV = (TextView) rootView.findViewById(R.id.kenh_tv_detail_favorite_child);
		time = (TextView) rootView.findViewById(R.id.time_detail_favorite_child);
		doi1 = (TextView) rootView.findViewById(R.id.doi1_detail_favorite_child);
		doi2 = (TextView) rootView.findViewById(R.id.doi2_detail_favorite_child);
		groupDetail = (TextView) rootView.findViewById(R.id.group_detail_favorite_child);
		groupDetail.setVisibility(View.INVISIBLE);
		//
		imgDoi1 = (ImageView) rootView.findViewById(R.id.imagedoi1_detail_favorite_child);
		imgDoi2 = (ImageView) rootView.findViewById(R.id.imagedoi2_detail_favorite_child);
		imageBtBack = (ImageButton) rootView.findViewById(R.id.imageButtonBack_itemfavorite_child);
		imageButtonArlam = (ImageButton) rootView.findViewById(R.id.alarm_detail_favorite_child);
		
		//
		//mMySQLiteManager = new MySQLiteManager(getActivity());
		ballTeamObj = new ArrayList<FootBallTeamObj>();
		ballTeamObj2  = new ArrayList<FootBallTeamObj>();
		imageButtonArlam.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String detail = matchScheduleObj.getDoi1() + " vs " + matchScheduleObj.getDoi2() + "\n" + matchScheduleObj.getGio() +" "+ matchScheduleObj.getNgay();
				String[] time = matchScheduleObj.getGio().split(":");
				int hour = Integer.valueOf(time[0].trim());
				int min = Integer.valueOf(time[1].trim());
				String[] datearray  = matchScheduleObj.getNgay().split(",");
				String date = datearray[0];
				String[] arraytime = date.split("/");
				int year = 2014;
				int month = Integer.valueOf(arraytime[1].trim())-1;				
				int day = Integer.valueOf(arraytime[0].trim());
				//
				//
				ShowDialog(year, month, day, hour, min, detail);
			}
		});		
		
		//
		displayImageOptions = new DisplayImageOptions.Builder()
		.cacheInMemory(false)
		.cacheOnDisc(true)		
		.resetViewBeforeLoading(false)
		.bitmapConfig(Bitmap.Config.RGB_565)										
		.showImageForEmptyUri(R.drawable.ic_emblem_club_placeholder)
		.showImageOnFail(R.drawable.ic_emblem_club_placeholder)		
		.imageScaleType(ImageScaleType.EXACTLY)										
		.build();
		//
		LoadData();
		imageBtBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				FavoriteChildFM.switchFragment(R.id.container_favorite_items, ListViewChildFM.newInstance(nameTeam, imgLink) , true , WCTAG.TAGFM_HOMEPL);
			}
		});
		return rootView;
	}
	
	public void LoadData()
	{
//		if(mTypeShow == WCTAG.TYPE_SHOW_VONGBANG)
//		{
			date.setText(((VongBangObj)matchScheduleObj).getNgay());
			time.setText(((VongBangObj)matchScheduleObj).getGio());
			kenhTV.setText(((VongBangObj)matchScheduleObj).getKenhTV());
			doi1.setText(((VongBangObj)matchScheduleObj).getDoi1());
			doi2.setText(((VongBangObj)matchScheduleObj).getDoi2());
			//			
			//
			ImageLoader.getInstance().displayImage(((VongBangObj)matchScheduleObj).getImgLinkDoi1(), imgDoi1, displayImageOptions);
			ImageLoader.getInstance().displayImage(((VongBangObj)matchScheduleObj).getImgLinkdoi2(), imgDoi2, displayImageOptions);
			//
	}
	//
	//------------------------------------------------------------------------------
	//

	public MatchScheduleObj getMatchScheduleObj() {
		return matchScheduleObj;
	}

	public void setMatchScheduleObj(MatchScheduleObj matchScheduleObj) {
		this.matchScheduleObj = matchScheduleObj;
	}

	public void ShowDialog(final int year,final int month,final int day,final int hour,final int min,final String detail)
	{
		final Dialog dialog = new Dialog(getActivity());
		dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
		dialog.setContentView(R.layout.dialog_alarm);
		Button buttonOk, cancel;
		final TimePicker timePicker = (TimePicker) dialog.findViewById(R.id.time_choose);
		
		timePicker.setCurrentHour(hour -1);
		timePicker.setCurrentMinute(min);
		buttonOk = (Button) dialog.findViewById(R.id.ok_btn_timechoose);
		cancel = (Button) dialog.findViewById(R.id.cancel_btn_timechoose);
		
		//timePicker.setCurrentHour();
		cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		buttonOk.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				int hourSet, minSet;
				hourSet = timePicker.getCurrentHour();
				minSet = timePicker.getCurrentMinute();				
				alarmReceiver = new AlarmReceiver();
//				
				alarmReceiver.setDay(day);
				alarmReceiver.setHour(hourSet);
				alarmReceiver.setMinute(minSet);				
				alarmReceiver.setMonth(month);
				alarmReceiver.setYear(year);		
				alarmReceiver.setDetail(detail);
				//
				alarmReceiver.setAlarm(getActivity());	
				Log.d("tag", hourSet +":" + minSet+"-- "+month+ "/"+day+"/" +year);
				int m = month+1;
				Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.choose_time_msg)+" " + hourSet +":" + minSet+"- "+day+ "/"+m, Toast.LENGTH_LONG).show();
				dialog.dismiss();
			}
			
		});
		dialog.show();
	}
	

}
